var vm;

var sendmsg = function() {
	// 定义消息类型,文字消息使用 RongIMLib.TextMessage
	var msg = new RongIMLib.TextMessage({
		content: vm.content,
		extra: "附加信息"
	});
	vm.messages.push({
		user: "draggy",
		content: vm.content
	});
	//或者使用RongIMLib.TextMessage.obtain 方法.具体使用请参见文档
	//var msg = RongIMLib.TextMessage.obtain("hello");
	var conversationtype = RongIMLib.ConversationType.PRIVATE; // 私聊
	var targetId = "piggy_1"; // 目标 Id
	RongIMClient.getInstance().sendMessage(conversationtype, targetId, msg, {
		// 发送消息成功
		onSuccess: function(message) {
			//message 为发送的消息对象并且包含服务器返回的消息唯一Id和发送消息时间戳
			console.log("Send successfully");
		},
		onError: function(errorCode, message) {
			var info = '';
			switch(errorCode) {
				case RongIMLib.ErrorCode.TIMEOUT:
					info = '超时';
					break;
				case RongIMLib.ErrorCode.UNKNOWN_ERROR:
					info = '未知错误';
					break;
				case RongIMLib.ErrorCode.REJECTED_BY_BLACKLIST:
					info = '在黑名单中，无法向对方发送消息';
					break;
				case RongIMLib.ErrorCode.NOT_IN_DISCUSSION:
					info = '不在讨论组中';
					break;
				case RongIMLib.ErrorCode.NOT_IN_GROUP:
					info = '不在群组中';
					break;
				case RongIMLib.ErrorCode.NOT_IN_CHATROOM:
					info = '不在聊天室中';
					break;
				default:
					info = x;
					break;
			}
			console.log('发送失败:' + info);
		}
	});
};

new Vue({
	el: 'html',
	data: {
		piggy_1: {
			userId: "piggy_1",
			token: "fH1tsRKCbHqZ3tT9W3idgg+9qsID0WPCIPKNWAcVlGnaqC6YY8XyOrE2s11FTR2SX86IC5OmK9W7PbJn8KUMSQPwSUDE2Gdv",
			name: "小猪"
		},
		draggy_1: {
			userId: "draggy_1",
			token: "EuffWG+MritIWNKS5hEhaA+9qsID0WPCIPKNWAcVlGnaqC6YY8XyOnunyiOzQw8otyRgfKh4eDrK7dAO6Qv2ZQPwSUDE2Gdv",
			name: "小小拽"
		},
		content: "",
		messages: [{
			user: "piggy",
			content: "hello~draggy!"
		}]
	},
	methods: {
		sendmsg: sendmsg
	},
	created: function() {
		vm = this;
	},
	ready: function() {
		RongIMClient.init("y745wfm84y1vv");

		// 设置连接监听状态 （ status 标识当前连接状态）
		// 连接状态监听器
		RongIMClient.setConnectionStatusListener({
			onChanged: function(status) {
				switch(status) {
					//链接成功
					case RongIMLib.ConnectionStatus.CONNECTED:
						console.log('链接成功');
						break;
						//正在链接
					case RongIMLib.ConnectionStatus.CONNECTING:
						console.log('正在链接');
						break;
						//重新链接
					case RongIMLib.ConnectionStatus.DISCONNECTED:
						console.log('断开连接');
						break;
						//其他设备登录
					case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
						console.log('其他设备登录');
						break;
						//网络不可用
					case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
						console.log('网络不可用');
						break;
				}
			}
		});

		// 消息监听器
		RongIMClient.setOnReceiveMessageListener({
			// 接收到的消息
			onReceived: function(message) {
				// 判断消息类型
				switch(message.messageType) {
					case RongIMClient.MessageType.TextMessage:
						// 发送的消息内容将会被打印
						console.log(message.content.content);
						vm.messages.push({
							user: "piggy",
							content: message.content.content
						});
						break;
					case RongIMClient.MessageType.VoiceMessage:
						// 对声音进行预加载                
						// message.content.content 格式为 AMR 格式的 base64 码
						RongIMLib.RongIMVoice.preLoaded(message.content.content);
						break;
					case RongIMClient.MessageType.ImageMessage:
						// do something...
						break;
					case RongIMClient.MessageType.DiscussionNotificationMessage:
						// do something...
						break;
					case RongIMClient.MessageType.LocationMessage:
						// do something...
						break;
					case RongIMClient.MessageType.RichContentMessage:
						// do something...
						break;
					case RongIMClient.MessageType.DiscussionNotificationMessage:
						// do something...
						break;
					case RongIMClient.MessageType.InformationNotificationMessage:
						// do something...
						break;
					case RongIMClient.MessageType.ContactNotificationMessage:
						// do something...
						break;
					case RongIMClient.MessageType.ProfileNotificationMessage:
						// do something...
						break;
					case RongIMClient.MessageType.CommandNotificationMessage:
						// do something...
						break;
					case RongIMClient.MessageType.CommandMessage:
						// do something...
						break;
					case RongIMClient.MessageType.UnknownMessage:
						// do something...
						break;
					default:
						// 自定义消息
						// do something...
				}
			}
		});

		var token = vm.draggy_1.token;

		RongIMClient.connect(token, {
			onSuccess: function(userId) {
				console.log("Login successfully." + userId);
			},
			onTokenIncorrect: function() {
				console.log('token无效');
			},
			onError: function(errorCode) {
				var info = '';
				switch(errorCode) {
					case RongIMLib.ErrorCode.TIMEOUT:
						info = '超时';
						break;
					case RongIMLib.ErrorCode.UNKNOWN_ERROR:
						info = '未知错误';
						break;
					case RongIMLib.ErrorCode.UNACCEPTABLE_PaROTOCOL_VERSION:
						info = '不可接受的协议版本';
						break;
					case RongIMLib.ErrorCode.IDENTIFIER_REJECTED:
						info = 'appkey不正确';
						break;
					case RongIMLib.ErrorCode.SERVER_UNAVAILABLE:
						info = '服务器不可用';
						break;
				}
				console.log(errorCode);
			}
		});
	}
});